package com.xinnet.mms.order.dao.impl;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository;

import com.xinnet.core.utils.page.PageView;
import com.xinnet.mms.allot.dao.impl.BaseDaoImpl;
import com.xinnet.mms.entity.AgentConsume;
import com.xinnet.mms.entity.Customer;
import com.xinnet.mms.entity.CustomerCare;
import com.xinnet.mms.entity.CustomerConsume;
import com.xinnet.mms.entity.CustomerConsumptionDetail;
import com.xinnet.mms.enums.ProductTypeEnum;
import com.xinnet.mms.order.dao.OrderDao;

@Repository("orderDao")
public class OrderDaoImpl extends BaseDaoImpl<Object, Object> implements OrderDao {

	/**
	 * 根据会员id查询该会员的所有订单
	 */
	@Override
	public List<CustomerCare> getOrdersByCusId(String cusId) throws SQLException {
		return this.getSqlSession().selectList("order.getOrdersByCusId",cusId);
	}

	/**
	 * 根据会员id以及当前时间查询该会员某个时间区间的所有订单
	 */
	@Override
	public List<CustomerCare> getOrdersByCusIdAndCurrentTime(String cusId, String timeSet) throws SQLException {
		Customer customer=new Customer();
		customer.setCusId(cusId);
		customer.setAddTime(timeSet);
		return this.getSqlSession().selectList("order.getOrdersByCusIdAndCurrentTime",customer);
	}

	/**
	 * 根据会员id以及产品ID查询该会员的订单
	 * @throws InterruptedException 
	 */
	@Override
	public List<CustomerCare> getOrdersByCusIdAndProductCode(String cusId, String productCode) throws Exception{
		SqlSession session = super.getSqlSession();
		Customer customer=new Customer();
		customer.setCusId(cusId);
		customer.setProductLabel(productCode);
		 List<CustomerCare>  customerCareList = session.selectList("order.getOrdersByCusIdAndProductId",customer);
		return customerCareList;

	}
	
	/**
	 * 获取新订单
	 * @param startTime
	 * @param endTime
	 * @return
	 * @throws SQLException
	 */
	public List<CustomerCare> getNewOrder(String startTime,String endTime)throws SQLException{
		CustomerCare customerCare=new CustomerCare();
		customerCare.setStartDueTime(startTime);
		customerCare.setEndDueTime(endTime);
		return this.getSqlSession().selectList("order.getNewOrder",customerCare);	
	}

	@Override
	public List<Customer> queryDueSoonGoods(PageView<Customer> pageView,
			Customer customer) {
		Map<Object, Object> map = new HashMap<Object, Object>();
		map.put("paging", pageView);
		Calendar cal = Calendar.getInstance(); 
		if(customer.getDueWarningTime()!=null){
			cal.add(customer.getDueWarningTime(), Calendar.DATE);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String vanishTimeStr = sdf.format(cal.getTime());
			customer.setVanishTime(vanishTimeStr);
		}
		
		
		map.put("t", customer);
		List<Customer> customerList = new ArrayList<Customer>();
		String productLabel = customer.getProductLabel();
		if(ProductTypeEnum.Hosting.getValue().equalsIgnoreCase(productLabel)) {
			customerList = getSqlSession().selectList("customer.queryIdc", map);
		}else if(ProductTypeEnum.Cloud.getValue().equalsIgnoreCase(productLabel) || ProductTypeEnum.VPS.getValue().equalsIgnoreCase(productLabel) ) {
			customerList = getSqlSession().selectList("customer.queryIdcServerHosting", map);
		}else if(ProductTypeEnum.Mailbox.getValue().equalsIgnoreCase(productLabel)) {
			customerList = getSqlSession().selectList("customer.queryMailbox", map);
		}else if(ProductTypeEnum.Vhost.getValue().equalsIgnoreCase(productLabel)) {
			customerList = getSqlSession().selectList("customer.queryVhost", map);
		}else if(ProductTypeEnum.ChiYun.getValue().equalsIgnoreCase(productLabel)) {
			customerList = getSqlSession().selectList("customer.queryChiYun", map);
		}else if(ProductTypeEnum.Domain.getValue().equalsIgnoreCase(productLabel)) {
			customerList = getSqlSession().selectList("customer.queryDomain", map);
		}else if(ProductTypeEnum.Jianzhan.getValue().equalsIgnoreCase(productLabel)) {
			customerList = getSqlSession().selectList("customer.queryJianzhan", map);
		}
		
		return customerList;
	}
	
	/**
	 * 获取新订单列表
	 * @param startTime
	 * @param endTime
	 * @return
	 * @throws SQLException
	 */
	public List<CustomerCare> getNewOrderList(String startTime,String endTime){
		CustomerCare customerCare=new CustomerCare();
		customerCare.setStartDueTime(startTime);
		customerCare.setEndDueTime(endTime);
		return this.getSqlSession().selectList("order.getNewOrderList",customerCare);	
	}

	@Override
	public Customer getServiceTimeByServiceIdAndProductCode(Customer customer) {
		
		return super.getSqlSession().selectOne("order.getServiceTimeByServiceIdAndProductCode", customer);
	}

	@Override
	public List<Customer> getOrderByCustomId(String customId) {
		Customer customer = new Customer();
		customer.setCustomId(customId);
		return super.getSqlSession().selectList("order.getOrderByCustomId", customer);
	}
	
	public List<CustomerCare>  getDeldOrderList(String cusId,String productCode){
		SqlSession session = super.getSqlSession();
		session.clearCache();
		Customer customer=new Customer();
		customer.setCusId(cusId);
		customer.setProductLabel(productCode);
		return session.selectList("order.getDeldOrderList",customer);
	}
	
	public List<CustomerCare>  getAllOrderList(){
		return this.getSqlSession().selectList("order.getAllOrderList");
	}

	@Override
	public Integer getDueSoonGoodsCount(Customer customer) {
		
		return super.getSqlSession().selectOne("customer.getDueSoonGoodsCount", customer);
	}
	
	@Override
	public List<Customer> getCusIdByExpireDate(Customer customer) {
		
		return super.getSqlSession().selectList("customer.getCusIdByExpireDate", customer);
	}
	
	
	@Override
	public List<Customer> getNYZJCusIdByExpireDate(Customer customer) {
		return super.getSqlSession().selectList("customer.getNYZJCusIdByExpireDate", customer);
	}

	@Override
	public List<Customer> getZJZYCusIdByExpireDate(Customer customer) {
		return super.getSqlSession().selectList("customer.getZJZYCusIdByExpireDate", customer);
	}

	@Override
	public List<Customer> getMCusIdByExpireDate(Customer customer) {
		return super.getSqlSession().selectList("customer.getMCusIdByExpireDate", customer);
	}

	@Override
	public List<Customer> getVCusIdByExpireDate(Customer customer) {
		return super.getSqlSession().selectList("customer.getVCusIdByExpireDate", customer);
	}

	@Override
	public List<Customer> getDCusIdByExpireDate(Customer customer) {
		return super.getSqlSession().selectList("customer.getDCusIdByExpireDate", customer);
	}

	@Override
	public List<Customer> getV_MEJZCusIdByExpireDate(Customer customer) {
		return super.getSqlSession().selectList("customer.getV_MEJZCusIdByExpireDate", customer);
	}

	@Override
	public List<Customer> getCHIYUNCusIdByExpireDate(Customer customer) {
		return super.getSqlSession().selectList("customer.getCHIYUNCusIdByExpireDate", customer);
	}
	

	@Override
	public List<Customer> queryIdcService(Customer customer) {
		return getSqlSession().selectList("customer.queryIdcService", customer);
	}

	@Override
	public List<Customer> queryIdcServerService(Customer customer) {
		return getSqlSession().selectList("customer.queryIdcServerService", customer);
	}

	@Override
	public List<Customer> queryMailboxService(Customer customer) {
		return getSqlSession().selectList("customer.queryMailboxService", customer);
	}

	@Override
	public List<Customer> queryOffice365Service(Customer customer) {
		return getSqlSession().selectList("customer.queryOffice365Service", customer);
	}

	@Override
	public List<Customer> queryNewOfficeService(Customer customer) {
		return getSqlSession().selectList("customer.queryNewOfficeService", customer);
	}

	@Override
	public List<Customer> queryVhostService(Customer customer) {
		return getSqlSession().selectList("customer.queryVhostService", customer);
	}

	/**
	 * 查询即将到期的驰云 
	 * @param pageView
	 * @param customer
	 * @return
	 */
	@Override
	public List<Customer> queryChiYunService(Customer customer){
		return getSqlSession().selectList("customer.queryChiYunService", customer);

	}

	@Override
	public List<Customer> queryDomainService(Customer customer) {
		return getSqlSession().selectList("customer.queryDomainService", customer);
	}

	@Override
	public List<Customer> queryJianzhanService(Customer customer) {
		return getSqlSession().selectList("customer.queryJianzhanService", customer);
	}
	
	@Override
	public List<Customer> queryIdc(PageView<Customer> pageView,
			Customer customer) {
		Integer startPage = pageView.getFirstResult();
		customer.setLimitTop(startPage);
		customer.setLimitDown(pageView.getPageSize());
		return getSqlSession().selectList("customer.queryIdc", customer);
	}

	@Override
	public List<Customer> queryIdcServerHosting(PageView<Customer> pageView,
			Customer customer) {
		Integer startPage = pageView.getFirstResult();
		customer.setLimitTop(startPage);
		customer.setLimitDown(pageView.getPageSize());
		return getSqlSession().selectList("customer.queryIdcServerHosting", customer);
	}

	@Override
	public List<Customer> queryMailbox(PageView<Customer> pageView,
			Customer customer) {
		Integer startPage = pageView.getFirstResult();
		customer.setLimitTop(startPage);
		customer.setLimitDown(pageView.getPageSize());
		return getSqlSession().selectList("customer.queryMailbox", customer);
	}

	@Override
	public List<Customer> queryVhost(PageView<Customer> pageView,
			Customer customer) {
		Integer startPage = pageView.getFirstResult();
		customer.setLimitTop(startPage);
		customer.setLimitDown(pageView.getPageSize());
		return getSqlSession().selectList("customer.queryVhost", customer);
	}

	/**
	 * 查询即将到期的驰云 
	 * @param pageView
	 * @param customer
	 * @return
	 */
	public List<Customer> queryChiYun(PageView<Customer> pageView, Customer customer){
		Integer startPage = pageView.getFirstResult();
		customer.setLimitTop(startPage);
		customer.setLimitDown(pageView.getPageSize());
		return getSqlSession().selectList("customer.queryChiYun", customer);

	}
		

	@Override
	public List<Customer> queryDomain(PageView<Customer> pageView,
			Customer customer) {
		Integer startPage = pageView.getFirstResult();
		customer.setLimitTop(startPage);
		customer.setLimitDown(pageView.getPageSize());
		return getSqlSession().selectList("customer.queryDomain", customer);
	}

	@Override
	public List<Customer> queryJianzhan(PageView<Customer> pageView,
			Customer customer) {
		Integer startPage = pageView.getFirstResult();
		customer.setLimitTop(startPage);
		customer.setLimitDown(pageView.getPageSize());
		return getSqlSession().selectList("customer.queryJianzhan", customer);
	}

	@Override
	public List<Customer> queryBuyGoods(PageView<Customer> pageView,
			Customer customer) {
		Integer startPage = pageView.getFirstResult();
		customer.setLimitTop(startPage);
		customer.setLimitDown(pageView.getPageSize());
		return super.getSqlSession().selectList("customer.queryBuyGoods", customer);
	}

	@Override
	public List<Customer> queryOrderByCustomId(PageView<Customer> pageView,
			Customer customer) {
		Integer startPage = pageView.getFirstResult();
		customer.setLimitTop(startPage);
		customer.setLimitDown(pageView.getPageSize());
		return super.getSqlSession().selectList("customer.queryOrderByCustomId", customer);
	}
	
	@Override
	public Integer queryBuyGoodsCount(
			Customer customer) {
		return super.getSqlSession().selectOne("customer.queryBuyGoodsCount", customer);
	}

	@Override
	public Integer queryOrderByCustomIdCount(
			Customer customer) {
		return super.getSqlSession().selectOne("customer.queryOrderByCustomIdCount", customer);
	}
	
	public List<CustomerCare>  getJianZhanOrder(){
		return super.getSqlSession().selectList("order.getJianZhanOrder");
	}
	
	public List<CustomerCare>  getDomainOrder(){
		return super.getSqlSession().selectList("order.getDomainOrder");
	}
	
	public List<CustomerCare>  getVirtualHostOrder(){
		return super.getSqlSession().selectList("order.getVirtualHostOrder");
	}
	
	public List<CustomerCare>  getMailOrder(){
		return super.getSqlSession().selectList("order.getMailOrder");
	}
	
	public List<CustomerCare>  getZJZYOrder(){
		return super.getSqlSession().selectList("order.getZJZYOrder");
	}
	
	public List<CustomerCare>  getNYZJOrXNYYOrderAll(){
		return super.getSqlSession().selectList("order.getNYZJOrXNYYOrderAll");
	}
	
	public List<CustomerCare>  getJianZhanOrderAll(){
		return super.getSqlSession().selectList("order.getJianZhanOrderAll");
	}
	
	public List<CustomerCare>  getDomainOrderAll(){
		return super.getSqlSession().selectList("order.getDomainOrderAll");
	}
	
	public List<CustomerCare>  getVirtualHostOrderAll(){
		return super.getSqlSession().selectList("order.getVirtualHostOrderAll");
	}
	
	public List<CustomerCare>  getMailOrderAll(){
		return super.getSqlSession().selectList("order.getMailOrderAll");
	}
	
	public List<CustomerCare>  getZJZYOrderAll(){
		return super.getSqlSession().selectList("order.getZJZYOrderAll");
	}
	
	public List<CustomerCare>  getNYZJOrXNYYOrder(){
		return super.getSqlSession().selectList("order.getNYZJOrXNYYOrder");
	}
	
	public List<CustomerCare>  getDeledDomainOrder(){
		return super.getSqlSession().selectList("order.getDeledDomainOrder");
		
	}
	
	public List<CustomerCare>  getDeledVirtualHostOrder(){
		return super.getSqlSession().selectList("order.getDeledVirtualHostOrder");
	}
	
	public List<CustomerCare>  getDeledMailOrder(){
		return super.getSqlSession().selectList("order.getDeledMailOrder");
	}

	@Override
	public Integer getIdcCount(Customer customer) {
		
		return super.getSqlSession().selectOne("customer.getIdcCount", customer);
	}

	@Override
	public Integer getIdcServerHostingCount(Customer customer) {
		
		return super.getSqlSession().selectOne("customer.getIdcServerHostingCount", customer);
	}

	@Override
	public Integer getMailboxCount(Customer customer) {
		
		return super.getSqlSession().selectOne("customer.getMailboxCount", customer);
	}

	@Override
	public Integer getVhostCount(Customer customer) {
		
		return super.getSqlSession().selectOne("customer.getVhostCount", customer);
	}

	@Override
	public Integer getChiYunCount(Customer customer) {
		
		return super.getSqlSession().selectOne("customer.getChiYunCount", customer);
	}

		
	@Override
	public Integer getDomainCount(Customer customer) {
		
		return super.getSqlSession().selectOne("customer.getDomainCount", customer);
	}

	@Override
	public Integer getJianzhanCount(Customer customer) {
		
		return super.getSqlSession().selectOne("customer.getJianzhanCount", customer);
	}

	@Override
	public List<CustomerConsumptionDetail> getCustomerConsumptionDetail(
			PageView<CustomerConsumptionDetail> pageView,
			CustomerConsume customerConsume) {
		Integer startPage = pageView.getFirstResult();
		customerConsume.setLimitTop(startPage);
		customerConsume.setLimitDown(pageView.getPageSize());
		return super.getSqlSession().selectList("custommix.queryCustomerConsumptionDetail", customerConsume);
	}

	@Override
	public int getCCDetailCount(CustomerConsume customerConsume) {
		return super.getSqlSession().selectOne("custommix.customerConsumptionDetailCount",customerConsume);
	}

	@Override
	public List<AgentConsume> getAgentConsume(CustomerConsume customerConsume) {
		return super.getSqlSession().selectList("custommix.doExpDealConsumptionDetail", customerConsume);
	}

	
}
